package com.uenpay.im.ui.old.activity.movement;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.aswife.ui.MaskImageView;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.configuration.APIConfiguration;

import java.util.ArrayList;

/**
 * @author SL
 * @created 2016-1-21 下午1:55:15
 * @copyRight http://www.qiyunxin.com
 * @function TODO
 */
public class MovementListAdapter extends BaseAdapter {

	private ArrayList<MovementEntity> arrayList = new ArrayList<MovementEntity>();
	private LayoutInflater layoutInflater = null;
	private Context mContext;

	public MovementListAdapter(Context context,
			ArrayList<MovementEntity> _arrayList) {
		arrayList = _arrayList;
		mContext = context;
		layoutInflater = LayoutInflater.from(context);
	}

	public void setData(ArrayList<MovementEntity> _arrayList) {
		arrayList = _arrayList;
		notifyDataSetChanged();
	}

	public void addData(ArrayList<MovementEntity> _arrayList) {
		arrayList.addAll(_arrayList);
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return arrayList == null ? 0 : arrayList.size();
	}

	@Override
	public Object getItem(int arg0) {
		return arrayList == null ? null : arrayList.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		MovementViewHodler hodler = null;
		MovementEntity movementEntity = arrayList.get(arg0);
		if (arg1 == null) {
			hodler = new MovementViewHodler();
			arg1 = layoutInflater.inflate(R.layout.item_movement_list_layout,
					null);
			hodler.titleTv = (TextView) arg1.findViewById(R.id.title_tv);
			hodler.publishDateTv = (TextView) arg1
					.findViewById(R.id.published_date_tv);
			hodler.startDateTv = (TextView) arg1
					.findViewById(R.id.start_date_tv);
			hodler.addressTv = (TextView) arg1.findViewById(R.id.address_tv);
			hodler.nickNameTv = (TextView) arg1.findViewById(R.id.nick_name_tv);
			hodler.freeTv = (TextView) arg1.findViewById(R.id.free_tv);
			hodler.start_second_tv = (TextView) arg1
					.findViewById(R.id.start_second_tv);
			hodler.start_week_tv = (TextView) arg1
					.findViewById(R.id.start_week_tv);
			hodler.avatarIv = (MaskImageView) arg1.findViewById(R.id.avatar_iv);
			arg1.setTag(hodler);
		} else {
			hodler = (MovementViewHodler) arg1.getTag();
		}
		hodler.titleTv.setText(movementEntity.movementTitle);
		hodler.addressTv.setText(mContext.getResources().getString(
				R.string.movement_address)
				+ "	" + movementEntity.movementAddress);
		hodler.publishDateTv.setText(movementEntity.movementCreateTime
				+ mContext.getString(R.string.movement_publish));
		hodler.startDateTv.setText(mContext.getResources().getString(
				R.string.movement_date)
				+ "	" + movementEntity.movementStartTime);
		hodler.start_week_tv.setText(movementEntity.movementPublishWeekDay);
		hodler.start_second_tv.setText(movementEntity.movementPublishSecond);
		String remarkName = IMApplication.config
				.getFriendsRemarkName(movementEntity.movementPublishCustId);
		if (TextUtils.isEmpty(remarkName)) {
			hodler.nickNameTv.setText(movementEntity.movementPublishNickName);
		} else {
			hodler.nickNameTv.setText(remarkName);
		}
		double fee = Double.valueOf(movementEntity.movementFee);

		if (fee > 0) {
			hodler.freeTv.setText(movementEntity.movementFee
					+ mContext.getResources().getString(
							R.string.movement_personal));
		} else {
			hodler.freeTv.setText(mContext.getResources().getString(
					R.string.movement_free_charge));
		}
		hodler.avatarIv.SetUrl(APIConfiguration.getFileSmallDownloadPath(
				movementEntity.movementCoverFileId,
				movementEntity.movementCoverFileHash));
		return arg1;
	}

	private class MovementViewHodler {
		private TextView titleTv, publishDateTv, startDateTv, addressTv,
				nickNameTv, freeTv, start_second_tv, start_week_tv;
		private MaskImageView avatarIv;
	}

}
